package com.xbongbong.workflow.service.operator.impl;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.enums.LogicalSymbolEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.pojo.workflow.WorkflowDataPojo;
import com.xbongbong.paas.pojo.workflow.WorkflowExplainPojo;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.util.FormDataUtil;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.enums.workflow.WorkflowValueTypeEnum;
import com.xbongbong.workflow.domain.entity.WorkflowNodeEntity;
import com.xbongbong.workflow.pojo.WorkflowOperationsPojo;
import com.xbongbong.workflow.pojo.WorkflowTransferPOJO;
import com.xbongbong.workflow.service.operator.WorkflowOperatorFieldHandler;
import com.xbongbong.workflow.service.operator.WorkflowOperatorService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2021/06/18 15:09
 */
@Service("workflowDynamicValueOperatorServiceImpl")
public class WorkflowDynamicValueOperatorServiceImpl implements WorkflowOperatorService {

    private static final Logger LOGGER = LoggerFactory.getLogger(WorkflowDynamicValueOperatorServiceImpl.class);

    @Resource
    private Map<Integer, WorkflowOperatorFieldHandler> workflowOperatorFieldMap;


    @Override
    public Integer type() {
        return WorkflowValueTypeEnum.DYNAMIC_VALUE.getType();
    }

    @Override
    public Object operatorByInsertNodeSourceData(WorkflowNodeEntity workflowNodeEntity, FieldAttrEntity fieldAttrEntity, FieldAttrEntity targetField, Map<Long, PaasFormDataEntityExt> formDataMap,
                           Map<Long, JSONObject> dataMap, WorkflowOperationsPojo workflowOperationsPojo, WorkflowDataPojo workflowDataPojo, WorkflowTransferPOJO workflowTransferPOJO, WorkflowExplainPojo workflowExplainPojo) throws XbbException {

        try {
            PaasFormDataEntityExt paasFormDataEntity = formDataMap.get(workflowOperationsPojo.getWorkflowNodeId());
            JSONObject data = dataMap.get(workflowOperationsPojo.getWorkflowNodeId());
            WorkflowOperatorFieldHandler workflowOperatorFieldHandler = workflowOperatorFieldMap.get(fieldAttrEntity.getFieldType());
            Object value = workflowOperatorFieldHandler.handlerThisFormField(paasFormDataEntity, data, fieldAttrEntity, workflowDataPojo, workflowOperationsPojo);
            workflowDataPojo.setValue(value);
            workflowOperatorFieldHandler = workflowOperatorFieldMap.get(targetField.getFieldType());
            return workflowOperatorFieldHandler.handlerTargetFormField(paasFormDataEntity, data, fieldAttrEntity, targetField, workflowDataPojo, workflowOperationsPojo, workflowNodeEntity);

        } catch (XbbException e) {
            throw e;
        } catch (Exception e) {
            LOGGER.error("WorkflowDynamicValueOperatorServiceImpl.operatorByInsertNodeSourceData error", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
        }
    }

    @Override
    public Object operatorByUpdateNode(WorkflowNodeEntity workflowNodeEntity, FieldAttrEntity fieldAttrEntity, FieldAttrEntity targetField, Map<Long, PaasFormDataEntityExt> formDataMap,
                                       Map<Long, JSONObject> dataMap, WorkflowOperationsPojo workflowOperationsPojo, WorkflowDataPojo workflowDataPojo, WorkflowTransferPOJO workflowTransferPOJO, WorkflowExplainPojo workflowExplainPojo) throws XbbException {
        try {
            JSONObject data = dataMap.get(workflowOperationsPojo.getWorkflowNodeId());
            PaasFormDataEntity thisFormDataEntity = formDataMap.get(workflowOperationsPojo.getWorkflowNodeId());
            if (Objects.isNull(fieldAttrEntity.getParentAttr())) {
                // 如果是主字段，则需要拿下主数据
                data = thisFormDataEntity.getData();
            }
            JSONObject targetData = dataMap.get(workflowDataPojo.getWorkflowNodeId());
            PaasFormDataEntity targetFormData = formDataMap.get(workflowDataPojo.getWorkflowNodeId());
            if (Objects.isNull(targetField.getParentAttr())) {
                // 如果是主字段，则需要拿下主数据
                targetData = targetFormData.getData();
            }
            WorkflowOperatorFieldHandler workflowOperatorFieldHandler = workflowOperatorFieldMap.get(fieldAttrEntity.getFieldType());
            Object value = workflowOperatorFieldHandler.handlerThisFormField(thisFormDataEntity, data, fieldAttrEntity, workflowDataPojo, workflowOperationsPojo);
            workflowDataPojo.setValue(value);
            workflowOperatorFieldHandler = workflowOperatorFieldMap.get(targetField.getFieldType());
            value = workflowOperatorFieldHandler.handlerTargetFormField(targetFormData, data, fieldAttrEntity, targetField, workflowDataPojo, workflowOperationsPojo, workflowNodeEntity);
            if (!Objects.equals(LogicalSymbolEnum.EQUAL.getSymbol(), workflowOperationsPojo.getSymbol())) {
                return FormDataUtil.count(targetField, targetData, targetField.getAttr(), value, workflowOperationsPojo.getSymbol());
            }
            return value;
        } catch (XbbException e) {
            throw e;
        }catch (Exception e) {
            LOGGER.error("WorkflowFixedValueOperatorServiceImpl.operatorByUpdateNode", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
        }
    }
}
